package com.flowershop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.flowershop.entity.CreativeProduct;
import com.flowershop.entity.Flower;
import com.flowershop.service.CreativeProductService;
import com.flowershop.service.CustomerService;
import com.flowershop.service.FlowerService;

@RestController
@RequestMapping("/api/dashboard")
@CrossOrigin
public class DashboardController {

    @Autowired
    private FlowerService flowerService;

    @Autowired
    private CreativeProductService creativeProductService;

    @Autowired
    private CustomerService customerService;

    private static final int STOCK_WARNING_THRESHOLD = 10;
    private static final int RECENT_ITEMS_LIMIT = 5;

    @GetMapping("/statistics")
    public Map<String, Object> getStatistics() {
        Map<String, Object> result = new HashMap<>();

        // 获取商品总数
        long flowerCount = flowerService.count();
        long productCount = creativeProductService.count();
        result.put("flowerCount", flowerCount);
        result.put("productCount", productCount);

        // 获取客户总数
        long customerCount = customerService.count();
        result.put("customerCount", customerCount);

        // 获取库存预警商品数量（库存小于10的商品）
        LambdaQueryWrapper<Flower> flowerWarningQuery = Wrappers.<Flower>lambdaQuery()
                .lt(Flower::getStock, STOCK_WARNING_THRESHOLD);
        long flowerWarningCount = flowerService.count(flowerWarningQuery);

        LambdaQueryWrapper<CreativeProduct> productWarningQuery = Wrappers.<CreativeProduct>lambdaQuery()
                .lt(CreativeProduct::getStock, STOCK_WARNING_THRESHOLD);
        long productWarningCount = creativeProductService.count(productWarningQuery);

        result.put("stockWarningCount", flowerWarningCount + productWarningCount);

        // 获取最近添加的商品
        LambdaQueryWrapper<Flower> recentFlowersQuery = Wrappers.<Flower>lambdaQuery()
                .orderByDesc(Flower::getCreatedTime)
                .last("LIMIT " + RECENT_ITEMS_LIMIT);
        List<Flower> recentFlowers = flowerService.list(recentFlowersQuery);
        result.put("recentFlowers", recentFlowers);

        // 获取库存最低的商品
        LambdaQueryWrapper<Flower> lowStockFlowersQuery = Wrappers.<Flower>lambdaQuery()
                .orderByAsc(Flower::getStock)
                .last("LIMIT " + RECENT_ITEMS_LIMIT);
        List<Flower> lowStockFlowers = flowerService.list(lowStockFlowersQuery);
        result.put("lowStockFlowers", lowStockFlowers);

        return result;
    }
} 